package com.dianping.cat.home.router.transform;

import com.dianping.cat.home.router.IEntity;
import com.dianping.cat.home.router.entity.DefaultServer;
import com.dianping.cat.home.router.entity.Domain;
import com.dianping.cat.home.router.entity.Group;
import com.dianping.cat.home.router.entity.GroupServer;
import com.dianping.cat.home.router.entity.Network;
import com.dianping.cat.home.router.entity.NetworkPolicy;
import com.dianping.cat.home.router.entity.RouterConfig;
import com.dianping.cat.home.router.entity.Server;
import com.dianping.cat.home.router.entity.ServerGroup;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Stack;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;










public class DefaultSaxParser
  extends DefaultHandler
{
/*  42 */   private DefaultLinker m_linker = new DefaultLinker(true);

/*  44 */   private DefaultSaxMaker m_maker = new DefaultSaxMaker();

/*  46 */   private Stack<String> m_tags = new Stack();

/*  48 */   private Stack<Object> m_objs = new Stack();

  private IEntity<?> m_entity;

/*  52 */   private StringBuilder m_text = new StringBuilder();


/*  55 */   public static RouterConfig parse(InputStream in) throws SAXException, IOException { return (RouterConfig)parseEntity(RouterConfig.class, new InputSource((InputStream)removeBOM(in))); }



/*  59 */   public static RouterConfig parse(Reader reader) throws SAXException, IOException { return (RouterConfig)parseEntity(RouterConfig.class, new InputSource((Reader)removeBOM(reader))); }



/*  63 */   public static RouterConfig parse(String xml) throws SAXException, IOException { return (RouterConfig)parseEntity(RouterConfig.class, new InputSource(new StringReader((String)removeBOM(xml)))); }



  private static <T extends IEntity<?>> T parseEntity(Class<T> type, InputSource is) throws SAXException, IOException {
    try {
/*  69 */       DefaultSaxParser handler = new DefaultSaxParser();
/*  70 */       SAXParserFactory factory = SAXParserFactory.newInstance();

/*  72 */       factory.setValidating(false);
/*  73 */       factory.setFeature("http://xml.org/sax/features/validation", false);

/*  75 */       factory.newSAXParser().parse(is, handler);
/*  76 */       return (T)handler.getEntity();
/*  77 */     } catch (ParserConfigurationException e) {
      throw new IllegalStateException("Unable to get SAX parser instance!", e);
    }
  }


/*  83 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, InputStream in) throws SAXException, IOException { return (T)parseEntity(type, new InputSource((InputStream)removeBOM(in))); }



/*  87 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, String xml) throws SAXException, IOException { return (T)parseEntity(type, new InputSource(new StringReader((String)removeBOM(xml)))); }




  private static <T> T removeBOM(T obj) throws IOException {
/*  93 */     if (obj instanceof String) {
/*  94 */       String str = (String)obj;

/*  96 */       if (str.length() != 0 && str.charAt(0) == '﻿') {
/*  97 */         return (T)str.substring(1);
      }
/*  99 */       return obj;
    }
/* 101 */     if (obj instanceof InputStream) {
/* 102 */       BufferedInputStream in = new BufferedInputStream((InputStream)obj);

/* 104 */       in.mark(3);

/* 106 */       if (in.read() != 239 || in.read() != 187 || in.read() != 191) {
/* 107 */         in.reset();
      }

/* 110 */       return (T)in;
/* 111 */     }  if (obj instanceof Reader) {
/* 112 */       BufferedReader in = new BufferedReader((Reader)obj);

/* 114 */       in.mark(1);

/* 116 */       if (in.read() != 65279) {
/* 117 */         in.reset();
      }

/* 120 */       return (T)in;
    }
/* 122 */     return obj;
  }



  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 128 */     if (value == null || value.length() == 0) {
/* 129 */       return defaultValue;
    }

/* 132 */     if (type == Boolean.class)
/* 133 */       return (T)Boolean.valueOf(value); 
/* 134 */     if (type == Integer.class)
/* 135 */       return (T)Integer.valueOf(value); 
/* 136 */     if (type == Long.class)
/* 137 */       return (T)Long.valueOf(value); 
/* 138 */     if (type == Short.class)
/* 139 */       return (T)Short.valueOf(value); 
/* 140 */     if (type == Float.class)
/* 141 */       return (T)Float.valueOf(value); 
/* 142 */     if (type == Double.class)
/* 143 */       return (T)Double.valueOf(value); 
/* 144 */     if (type == Byte.class)
/* 145 */       return (T)Byte.valueOf(value); 
/* 146 */     if (type == Character.class) {
/* 147 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 149 */     return (T)value;
  }




/* 155 */   public void characters(char[] ch, int start, int length) throws SAXException { this.m_text.append(ch, start, length); }




/* 160 */   public void endDocument() { this.m_linker.finish(); }



  public void endElement(String uri, String localName, String qName) throws SAXException {
/* 165 */     if (uri == null || uri.length() == 0) {
/* 166 */       this.m_objs.pop();
/* 167 */       this.m_tags.pop();
    }


/* 171 */     this.m_text.setLength(0);
  }


/* 175 */   private IEntity<?> getEntity() { return this.m_entity; }



/* 179 */   protected String getText() { return this.m_text.toString(); }


  private void parseForDefaultServer(DefaultServer parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 183 */     this.m_objs.push(parentObj);
/* 184 */     this.m_tags.push(qName);
  }

  private void parseForDomain(Domain parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 188 */     if ("group".equals(qName)) {
/* 189 */       Group group = this.m_maker.buildGroup(attributes);

/* 191 */       this.m_linker.onGroup(parentObj, group);
/* 192 */       this.m_objs.push(group);
    } else {
/* 194 */       throw new SAXException(String.format("Element(%s) is not expected under domain!", new Object[] { qName }));
    }

/* 197 */     this.m_tags.push(qName);
  }

  private void parseForGroup(Group parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 201 */     if ("server".equals(qName)) {
/* 202 */       Server server = this.m_maker.buildServer(attributes);

/* 204 */       this.m_linker.onServer(parentObj, server);
/* 205 */       this.m_objs.push(server);
    } else {
/* 207 */       throw new SAXException(String.format("Element(%s) is not expected under group!", new Object[] { qName }));
    }

/* 210 */     this.m_tags.push(qName);
  }

  private void parseForGroupServer(GroupServer parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 214 */     this.m_objs.push(parentObj);
/* 215 */     this.m_tags.push(qName);
  }

  private void parseForNetwork(Network parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 219 */     this.m_objs.push(parentObj);
/* 220 */     this.m_tags.push(qName);
  }

  private void parseForNetworkPolicy(NetworkPolicy parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 224 */     if ("network".equals(qName)) {
/* 225 */       Network network = this.m_maker.buildNetwork(attributes);

/* 227 */       this.m_linker.onNetwork(parentObj, network);
/* 228 */       this.m_objs.push(network);
    } else {
/* 230 */       throw new SAXException(String.format("Element(%s) is not expected under network-policy!", new Object[] { qName }));
    }

/* 233 */     this.m_tags.push(qName);
  }

  private void parseForRouterConfig(RouterConfig parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 237 */     if ("default-server".equals(qName)) {
/* 238 */       DefaultServer defaultServer = this.m_maker.buildDefaultServer(attributes);

/* 240 */       this.m_linker.onDefaultServer(parentObj, defaultServer);
/* 241 */       this.m_objs.push(defaultServer);
/* 242 */     } else if ("network-policy".equals(qName)) {
/* 243 */       NetworkPolicy networkPolicy = this.m_maker.buildNetworkPolicy(attributes);

/* 245 */       this.m_linker.onNetworkPolicy(parentObj, networkPolicy);
/* 246 */       this.m_objs.push(networkPolicy);
/* 247 */     } else if ("server-group".equals(qName)) {
/* 248 */       ServerGroup serverGroup = this.m_maker.buildServerGroup(attributes);

/* 250 */       this.m_linker.onServerGroup(parentObj, serverGroup);
/* 251 */       this.m_objs.push(serverGroup);
/* 252 */     } else if ("domain".equals(qName)) {
/* 253 */       Domain domain = this.m_maker.buildDomain(attributes);

/* 255 */       this.m_linker.onDomain(parentObj, domain);
/* 256 */       this.m_objs.push(domain);
    } else {
/* 258 */       throw new SAXException(String.format("Element(%s) is not expected under router-config!", new Object[] { qName }));
    }

/* 261 */     this.m_tags.push(qName);
  }

  private void parseForServer(Server parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 265 */     this.m_objs.push(parentObj);
/* 266 */     this.m_tags.push(qName);
  }

  private void parseForServerGroup(ServerGroup parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 270 */     if ("group-server".equals(qName)) {
/* 271 */       GroupServer groupServer = this.m_maker.buildGroupServer(attributes);

/* 273 */       this.m_linker.onGroupServer(parentObj, groupServer);
/* 274 */       this.m_objs.push(groupServer);
    } else {
/* 276 */       throw new SAXException(String.format("Element(%s) is not expected under server-group!", new Object[] { qName }));
    }

/* 279 */     this.m_tags.push(qName);
  }

  private void parseRoot(String qName, Attributes attributes) throws SAXException {
/* 283 */     if ("router-config".equals(qName)) {
/* 284 */       RouterConfig routerConfig = this.m_maker.buildRouterConfig(attributes);

/* 286 */       this.m_entity = routerConfig;
/* 287 */       this.m_objs.push(routerConfig);
/* 288 */       this.m_tags.push(qName);
/* 289 */     } else if ("default-server".equals(qName)) {
/* 290 */       DefaultServer defaultServer = this.m_maker.buildDefaultServer(attributes);

/* 292 */       this.m_entity = defaultServer;
/* 293 */       this.m_objs.push(defaultServer);
/* 294 */       this.m_tags.push(qName);
/* 295 */     } else if ("network-policy".equals(qName)) {
/* 296 */       NetworkPolicy networkPolicy = this.m_maker.buildNetworkPolicy(attributes);

/* 298 */       this.m_entity = networkPolicy;
/* 299 */       this.m_objs.push(networkPolicy);
/* 300 */       this.m_tags.push(qName);
/* 301 */     } else if ("network".equals(qName)) {
/* 302 */       Network network = this.m_maker.buildNetwork(attributes);

/* 304 */       this.m_entity = network;
/* 305 */       this.m_objs.push(network);
/* 306 */       this.m_tags.push(qName);
/* 307 */     } else if ("server-group".equals(qName)) {
/* 308 */       ServerGroup serverGroup = this.m_maker.buildServerGroup(attributes);

/* 310 */       this.m_entity = serverGroup;
/* 311 */       this.m_objs.push(serverGroup);
/* 312 */       this.m_tags.push(qName);
/* 313 */     } else if ("group-server".equals(qName)) {
/* 314 */       GroupServer groupServer = this.m_maker.buildGroupServer(attributes);

/* 316 */       this.m_entity = groupServer;
/* 317 */       this.m_objs.push(groupServer);
/* 318 */       this.m_tags.push(qName);
/* 319 */     } else if ("domain".equals(qName)) {
/* 320 */       Domain domain = this.m_maker.buildDomain(attributes);

/* 322 */       this.m_entity = domain;
/* 323 */       this.m_objs.push(domain);
/* 324 */       this.m_tags.push(qName);
/* 325 */     } else if ("group".equals(qName)) {
/* 326 */       Group group = this.m_maker.buildGroup(attributes);

/* 328 */       this.m_entity = group;
/* 329 */       this.m_objs.push(group);
/* 330 */       this.m_tags.push(qName);
/* 331 */     } else if ("server".equals(qName)) {
/* 332 */       Server server = this.m_maker.buildServer(attributes);

/* 334 */       this.m_entity = server;
/* 335 */       this.m_objs.push(server);
/* 336 */       this.m_tags.push(qName);
    } else {
/* 338 */       throw new SAXException("Unknown root element(" + qName + ") found!");
    }
  }


  public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
/* 344 */     if (uri == null || uri.length() == 0) {
/* 345 */       if (this.m_objs.isEmpty()) {
/* 346 */         parseRoot(qName, attributes);
      } else {
/* 348 */         Object parent = this.m_objs.peek();
/* 349 */         String tag = (String)this.m_tags.peek();

/* 351 */         if (parent instanceof RouterConfig) {
/* 352 */           parseForRouterConfig((RouterConfig)parent, tag, qName, attributes);
/* 353 */         } else if (parent instanceof DefaultServer) {
/* 354 */           parseForDefaultServer((DefaultServer)parent, tag, qName, attributes);
/* 355 */         } else if (parent instanceof NetworkPolicy) {
/* 356 */           parseForNetworkPolicy((NetworkPolicy)parent, tag, qName, attributes);
/* 357 */         } else if (parent instanceof Network) {
/* 358 */           parseForNetwork((Network)parent, tag, qName, attributes);
/* 359 */         } else if (parent instanceof ServerGroup) {
/* 360 */           parseForServerGroup((ServerGroup)parent, tag, qName, attributes);
/* 361 */         } else if (parent instanceof GroupServer) {
/* 362 */           parseForGroupServer((GroupServer)parent, tag, qName, attributes);
/* 363 */         } else if (parent instanceof Domain) {
/* 364 */           parseForDomain((Domain)parent, tag, qName, attributes);
/* 365 */         } else if (parent instanceof Group) {
/* 366 */           parseForGroup((Group)parent, tag, qName, attributes);
/* 367 */         } else if (parent instanceof Server) {
/* 368 */           parseForServer((Server)parent, tag, qName, attributes);
        } else {
/* 370 */           throw new RuntimeException(String.format("Unknown entity(%s) under %s!", new Object[] { qName, parent.getClass().getName() }));
        }
      }

/* 374 */       this.m_text.setLength(0);
    } else {
/* 376 */       throw new SAXException(String.format("Namespace(%s) is not supported by %s.", new Object[] { uri, getClass().getName() }));
    }
  }

  protected Date toDate(String str, String format) {
    try {
/* 382 */       return (new SimpleDateFormat(format)).parse(str);
/* 383 */     } catch (ParseException e) {
/* 384 */       throw new RuntimeException(String.format("Unable to parse date(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/router/transform/DefaultSaxParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */